package com.cj.common.response;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;

/**
 * @Author cheng jun
 * @Description:
 * @Date 2021/7/8 19:03
 */

@Getter
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;

    public Result() {
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(data);
    }
    public static <T> Result<T> error(CodeMsg codeMsg) {
        return new Result<T>(codeMsg);
    }

    private Result(T data) {
        this.data = data;
        this.code = CodeMsg.SUCCESS.getCode();
        this.msg = CodeMsg.SUCCESS.getMsg();
    }

    private Result(CodeMsg codeMsg) {
        if (codeMsg==null){
            return;
        }
        this.data = (T) new JSONObject();
        this.code = codeMsg.getCode();
        this.msg=codeMsg.getMsg();
    }

    @Getter
    public static class CodeMsg {
        private Integer code;
        private String msg;
        /**
         *通用的错误码
         */
        public static final CodeMsg SUCCESS = new CodeMsg(0, "success");
        public static final CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常");
        public static final CodeMsg BIND_ERROR = new CodeMsg(500101, "参数异常");

        /**
         * 登录模块 5002XX
         */
        public static CodeMsg SESSION_ERROR = new CodeMsg(500210, "Session不存在或者已经失效");
        public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "登录密码不能为空");
        public static CodeMsg MOBILE_EMPTY = new CodeMsg(500212, "手机号不能为空");
        public static CodeMsg MOBILE_ERROR = new CodeMsg(500213, "手机号格式错误");
        public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214, "手机号不存在");
        public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215, "密码错误");
        public static final CodeMsg VERIFICATION_CODE_ERROR = new CodeMsg(500216, "验证码不正确");
        public static final CodeMsg ACCOUNT_OR_PASSWORD_ERROR = new CodeMsg(500217, "账号或密码不正确");
        public static final CodeMsg ACCOUNT_IS_LOCKED_ERROR = new CodeMsg(500218, "账号已被锁定,请联系管理员");
        public static final CodeMsg SC_UNAUTHORIZED = new CodeMsg(500219, "invalid token");
        public static final CodeMsg TOKEN_EXPIRED = new CodeMsg(500220, "认证过期");
        public static final CodeMsg PLEASE_LOG_IN = new CodeMsg(500221, "请登陆");
        public static final CodeMsg AUTHENTICATION_FAILED = new CodeMsg(500222, "认证失败");

        /**
         * 文件上传异常
         */
        public static final CodeMsg FILE_TOO_BIG = new CodeMsg(500300, "文件过大");
        public static final CodeMsg UPLOAD_FAILED = new CodeMsg(500301, "上传失败");
        public static final CodeMsg FILE_TO_BASE64 = new CodeMsg(500302, "文件转base64失败");
        public static final CodeMsg GET_PATH_FAILED = new CodeMsg(500303, "获取路径失败");

        /**
         * openFeign调用异常
         */
        public static final CodeMsg OPEN_FEIGN_FAILED = new CodeMsg(500400, "openFeign调用失败");

        public static final CodeMsg SAVE_FAILED = new CodeMsg(500500, "保存失败");
        /**
         * 商品模块 5003XX
         */
        /**
         * 订单模块 5004XX
         */
        /**
         * 秒杀模块 5005XX
         */

        /**
         * 私有无参构造函数
         */
        private CodeMsg() {

        }
        /**
         * 构造函数私有化
         * 为了不让别人直接new出来
         *
         * @param code
         * @param msg
         */
        public CodeMsg(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }
        /**
         * 给CodeMsg里面的msg属性追加属性
         * @param args
         * @return
         */
        public CodeMsg fillArgs(Object... args) {
            int code=this.code;
            String message = String.format(this.msg, args);
            return new CodeMsg(code,message);
        }
    }
}